package com.rightsidetech.http.controller.userApp.massage;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.utils.DateUtil;
import com.rightsidetech.http.mq.MqService;
import com.rightsidetech.http.mq.ProducerMsg;
import com.rightsidetech.http.wechatutil.MsgTemplateParam;
import com.rightsidetech.http.wechatutil.MsgTemplate;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.config.WechatConfig;
import com.rightsidetech.http.wechatutil.MsgTemplatesEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.ir.annotations.Ignore;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;

/**
 * @Package: com.rightsidetech.http.controller.userApp.massage
 * @ClassName: WeChatSendMassageController
 * @Author: yinj
 * @CreateTime: 2021/12/20 11:20
 */
@Api(tags = "微信小程序消息订阅测试")
@RestController
@RequestMapping("/userApp/massage")
@ShenyuSpringMvcClient(path = "/userApp/massage/**")
@Slf4j
public class WeChatSendMassageController {

    @Autowired
    private WechatConfig wechatConfig;
    @Autowired
    private MqService mqService;

    @Autowired
    private ProducerMsg producerMsg;


    @Ignore
    @Log
    @GetMapping(value = "/send")
    @ApiOperation(value = "测试发送消息")
    public ResponseData sendMassage(String openid) {
        String params = "grant_type=client_credential&appid=" + wechatConfig.getAppId() + "&secret=" + wechatConfig.getSecret();
        String s = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?" + params);
        JSONObject jsonObject = JSONObject.parseObject(s);
        String accessToken = jsonObject.getString("access_token");

        MsgTemplate template = new MsgTemplate();
        template.setTemplate_id("3vZ_t70wGcdvPTzYgF4V1pZeunUNVpOKiLDv_9VGkss");
        template.setTouser(openid);
        HashMap<String, MsgTemplateParam> map = new HashMap<>();
        map.put("character_string4",new MsgTemplateParam( "666666"));
        map.put("time8",new MsgTemplateParam(DateUtil.getDate(new Date(),"yyyy年MM月dd日 HH:mm")));
        map.put("phone_number10",new MsgTemplateParam("15657116673"));

        template.setData(map);
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);

        System.out.println(JSON.toJSONString(template));
        String result = HttpUtil.post(requestUrl, JSON.toJSONString(template), 5000);
        if (result != null) {
            System.out.println(result);
            JSONObject parse = JSONObject.parseObject(result);
            int errorCode = parse.getIntValue("errcode");
            String errorMessage = parse.getString("errmsg");
            if (errorCode == 0) {
                System.out.println("Send Success");
            } else {
                System.out.println("订阅消息发送失败:" + errorCode + "," + errorMessage);
            }
        }

        return ResponseData.success();

    }

    @Log
    @GetMapping(value = "/send2")
    @ApiOperation(value = "测试订单待支付提醒发送消息")
    public ResponseData test(String orderNo) throws Exception{
        HashMap<String, String> map = new HashMap<>();
        map.put("typeId", MsgTemplatesEnum.CANCEL_ORDER.getTypeId().toString());
        map.put("orderNo",orderNo);
        mqService.sendOderMessage(JSONObject.toJSONString(map));
        return ResponseData.success();
    }


    @Log
    @GetMapping(value = "/send3")
    @ApiOperation(value = "测试订单待支付提醒-加入队列发送")
    public ResponseData test2(String orderNo) throws Exception{
        HashMap<String, String> map = new HashMap<>();
        map.put("typeId", MsgTemplatesEnum.CANCEL_ORDER.getTypeId().toString());
        map.put("orderNo",orderNo);
        producerMsg.sendMsgOrderMq(map,1);
        return ResponseData.success();
    }






}
